<template>
  <div class="container">
    <el-card class="box-card">
      <questionsFilter></questionsFilter>
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-table :data="list">
        <el-table-column prop="number" label="试题编号" />
        <el-table-column prop="subject" label="学科" />
        <el-table-column prop="catalog" label="目录" />
        <el-table-column
          :formatter="questionTypeFn"
          prop="questionType"
          label="题型"
        />
        <el-table-column prop="question" label="题干" width="230px" />
        <el-table-column
          prop="addDate"
          label="录入时间"
          fixed="right"
          width="280"
        >
          <template #default="{ row }">
            {{ formateTimeFn(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="difficulty"
          label="难度"
          :formatter="difficultyFn"
          fixed="right"
          width="140"
        />
        <el-table-column
          prop="creator"
          label="录入人"
          fixed="right"
          width="140"
        />
        <el-table-column label="操作" fixed="right" width="180px">
          <template #default="{ row }">
            <el-button
              @click="questionsdetail(row)"
              type="primary"
              size="small"
              title="预览"
              circle
              plain
            >
              <i class="el-icon-view"></i>
            </el-button>
            <el-button
              type="success"
              size="small"
              title="修改"
              @click="questionsupdate(row)"
              circle
              plain
            >
              <i class="el-icon-edit"></i>
            </el-button>
            <el-button
              @click="questionsremove(row)"
              type="danger"
              size="small"
              title="删除"
              circle
              plain
            >
              <i class="el-icon-delete"></i>
            </el-button>
            <el-button
              @click="questionschoiceAdd(row)"
              type="warning"
              size="small"
              title="加入精选"
              circle
              plain
            >
              <i class="el-icon-check"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :current-page="page"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog title="题目预览" :visible.sync="closeDialogFn">
      <el-row>
        <el-col :span="6"
          ><div>
            【题型】：{{ questionTypefn(detailList.questionType) }}
          </div></el-col
        >
        <el-col :span="6"
          ><div>【编号】：{{ detailList.id }}</div></el-col
        >
        <el-col :span="6"
          ><div>
            【难度】：{{
              difficulty.find(
                (item) => item.value === Number(detailList.difficulty)
              )
                ? difficulty.find(
                    (item) => item.value === Number(detailList.difficulty)
                  ).label
                : "未知"
            }}
          </div></el-col
        >
        <el-col :span="6"
          ><div>【标签】：{{ detailList.tags }}</div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="6"
          ><div>【学科】：{{ detailList.subjectName }}</div></el-col
        >
        <el-col :span="6"
          ><div>【目录】：{{ detailList.directoryName }}</div></el-col
        >
        <el-col :span="6"
          ><div>【方向】：{{ detailList.direction }}</div></el-col
        >
      </el-row>
      <el-divider></el-divider>
      <el-row>
        <el-col :span="24"><div>【题干】：</div></el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div style="height: 100%; width: 100%">
            {{ detailList.question }}
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div style="width: 100%; height: 100%">
            {{ questionTypefn(detailList.questionType) }}题
            选项：（以下选中的选项为正确答案）
          </div>
        </el-col>
      </el-row>
      <el-row>
        <div v-if="detailList.questionType === '1'">
          <el-radio-group v-model="radio">
            <div v-for="item in detailList.options" :key="item.code">
              <el-radio @change="radio = 1" :label="item.isRight">{{
                item.title
              }}</el-radio>
            </div>
          </el-radio-group>
        </div>
        <div v-if="detailList.questionType === '2'">
          <el-checkbox-group v-model="checkList">
            <div v-for="item in detailList.options" :key="item.code">
              <el-checkbox @change="checkList = [1]" :label="item.isRight">{{
                item.title
              }}</el-checkbox>
            </div>
          </el-checkbox-group>
        </div>
      </el-row>
      <el-divider></el-divider>
      <el-row>
        <el-col :span="24">
          <div style="height: 100%; width: 100%">
            【参考答案】：
            <el-button type="danger" @click="showVideo = !showVideo"
              >视频答案预览</el-button
            >
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div v-if="showVideo" style="height: 100%">
            <video
              src=""
              autoplay="autoplay"
              width="400px"
              controls="controls"
            ></video>
          </div>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row>
        <el-col :span="24"
          ><div style="height: 100%; width: 100%">
            【答案解析】：{{ detailList.answer }}
          </div></el-col
        >
      </el-row>
      <el-divider></el-divider>
      <el-row>
        <el-col :span="24"
          ><div style="height: 100%; width: 100%">
            【题目备注】：{{ detailList.remarks }}
          </div></el-col
        >
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeDialogFn = false"
          >关闭</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  list as questionslist,
  detail as questionsdetailApi,
  remove as questionsremoveApi,
  choiceAdd as questionschoiceAddApi
} from '@/api/hmmm/questions'
import { difficulty, questionType } from '@/api/hmmm/constants'
import questionsFilter from '../components/questions-filter.vue'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      // 当前页码
      page: 1,
      // 当前页容量
      pagesize: 5,
      // 表示的是当前页的数据
      list: [],
      // 一共有多少条数据
      detailList: [], // 预览题库详情
      total: 0,
      // 枚举
      difficulty, // 难度
      questionType, // 题型枚举
      closeDialogFn: false, // 预览弹出框
      showVideo: false, // 视频框是否出现
      radio: 1, // 点选多选选中
      checkList: [1]
    }
  },
  components: {
    questionsFilter
  },
  created () {
    this.getquestionsList()
  },
  methods: {
    // 获取基础题库列表 在生命周期钩子created中调用
    async getquestionsList () {
      const res = await questionslist({
        page: this.page,
        pagesize: this.pagesize
      })
      // console.log(res)

      res.data.items.forEach(item => {
        // console.log(item.question)
        item.question = item.question.replace(/<[^>]+>/g, '')
      })
      // console.log(arr)
      this.list = res.data.items
      this.total = res.data.counts
    },
    // 处理题型
    questionTypeFn (row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      // console.log(cellValue)

      const obj = questionType.find(item => item.value === Number(cellValue))
      return obj ? obj.label : '未知'
    },
    questionTypefn (cellValue) {
      // console.log(row, column, cellValue, index)
      // console.log(cellValue)
      const obj = questionType.find(item => item.value === Number(cellValue))
      return obj ? obj.label : '未知'
    },
    // 处理难度
    difficultyFn (row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      const obj = difficulty.find(item => item.value === Number(cellValue))
      return obj ? obj.label : '未知'
    },
    // 处理时间
    formateTimeFn (time) {
      // 处理成 年-月-日 并且返回
      return dayjs(time).format('YYYY-MM-DD hh:mm:ss')
    },
    // 查看预览
    async questionsdetail (row) {
      this.closeDialogFn = true
      const { data } = await questionsdetailApi(row)
      data.answer = data.answer.replace(/<[^>]+>/g, '')
      data.question = data.question.replace(/<[^>]+>/g, '')
      this.detailList = data
    },
    // 修改
    async questionsupdate (row) {
      this.$router.push(`/questions/new?id=${row.id}`)
      // const res = await questionsupdateApi(row)
      // console.log(row)
    },
    // 删除
    questionsremove (row) {
      this.$confirm('确定要删除该题吗？', '温馨提示')
        .then(async () => {
          await questionsremoveApi(row)
          this.$message.success('删除成功')
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          this.getquestionsList()
        })
        .catch(() => {})
    },
    // 加入精选题库
    questionschoiceAdd (row) {
      this.$confirm('此操作将该题目加入精选, 是否确定?', '温馨提示')
        .then(async () => {
          await questionschoiceAddApi({ ...row, choiceState: 1 })
          this.$message.success('加入精选成功')
          this.getquestionsList()
        })
        .catch(() => {})
    },
    // 页码发生变化的时候 触发该事
    handleCurrentChange (val) {
      this.page = val

      this.getquestionsList()
    },
    // 页容量发生变化的时候 触发该事件
    handleSizeChange (val) {
      this.pagesize = val
      // console.log(val)
      this.page = 1
      this.getquestionsList()
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 0 10px;
  margin: 10px 0;
  .el-col div {
    width: 183px;
    height: 36px;
  }
  .dialog-footer {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
